package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.result.Result;
import io.lettuce.core.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author: atguigu
 * @create: 2023-12-27 15:36
 */
@Slf4j
@RestController
@RequestMapping("/api/test")
public class TestController {


    @Autowired
    private RedissonClient redissonClient;

    /***
     * 用于测试发送延迟消息
     * @param msg
     * @param ttl 延迟时间 单位：s
     * @return
     */
    @GetMapping("/sendDealyMsg/{msg}/{ttl}")
    public Result sendDelayMsg(@PathVariable String msg, @PathVariable int ttl) {
        //1.创建阻塞队列（当队列内元素超过上限，继续队列发送消息，进入阻塞状态/当队列中元素为空，继续拉取消息，进入阻塞状态）
        RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue("myBlockingQueue");
        //2.基于阻塞队列创建延迟队列
        RDelayedQueue<String> delayedQueue = redissonClient.getDelayedQueue(blockingQueue);
        //3.调用延迟队列发送延迟消息方法
        delayedQueue.offer(msg, ttl, TimeUnit.SECONDS);
        log.info("发送延迟消息成功：{}，延迟时间：{}s", msg, ttl);
        return Result.ok();
    }
}
